package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func thaw(prefixStr string) {
	client, err := oss.New("https://oss-cn-zhangjiakou.aliyuncs.com", "LTAI4FsA8TiM6JATw1mmpvoV", "M641HGVBk2uiS2cpqS09Ul4o2ElaKg")
	if err != nil {
		log.Panicln(err)
	}

	bucket, err := client.Bucket("his-eis-ali")
	if err != nil {
		log.Panicln(err)
	}

	var restoreConfig oss.RestoreConfiguration
	restoreConfig.Tier = string(oss.RestoreExpedited)
	restoreConfig.Days = 1

	marker := oss.Marker("")
	prefix := oss.Prefix(prefixStr)
	for {
		lor, err := bucket.ListObjects(marker, prefix)
		if err != nil {
			log.Panicln(err)
		}
		log.Println("len", len(lor.Objects))
		for _, object := range lor.Objects {
			log.Println("Objects:", object.Key)
			meta, err := bucket.GetObjectDetailedMeta(object.Key)
			if err != nil {
				log.Panicln(err)
			}

			if meta.Get("X-Oss-Storage-Class") == string(oss.StorageArchive) {
				log.Println("归档", meta.Get("X-Oss-Restore"))
				if meta.Get("X-Oss-Restore") == "" {
					log.Println("冻结")
					if err := bucket.RestoreObject(object.Key); err != nil {
						log.Panicln(err)
					}
				}
			}
		}
		prefix = oss.Prefix(lor.Prefix)
		marker = oss.Marker(lor.NextMarker)
		if !lor.IsTruncated {
			break
		}
	}
}

func main() {
	// thaw("ksxd/parcel/2020/4")
	// thaw("ksxd/parcel/2020/5")
	// thaw("ksxd/parcel/2020/6")
	// thaw("ksxd/parcel/2020/7")
	// thaw("ksxd/parcel/2020/8")
	// thaw("ksxd/parcel/2020/9")
	// thaw("ksxd/parcel/2020/10")
	// thaw("ksxd/parcel/2020/11")
	// thaw("ksxd/parcel/2020/12")
	// thaw("ksxd/parcel/2021/1")
	// thaw("ksxd/parcel/2021/2")
	// thaw("free/parcel/2020/5/19")
	thaw("free/parcel/2020/5/20")
	thaw("free/parcel/2020/5/21")
	thaw("free/parcel/2020/5/22")
	thaw("free/parcel/2020/5/23")
	thaw("free/parcel/2020/5/24")
	thaw("free/parcel/2020/5/25")
	thaw("free/parcel/2020/5/26")
	thaw("free/parcel/2020/5/27")
	thaw("free/parcel/2020/5/28")
	thaw("free/parcel/2020/5/29")
	thaw("free/parcel/2020/5/30")
	thaw("free/parcel/2020/5/31")
	thaw("free/parcel/2020/6")
	thaw("free/parcel/2020/7")
	thaw("free/parcel/2020/8")
	thaw("free/parcel/2020/9")
	thaw("free/parcel/2020/10")
}
